﻿Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class SqlDataBase
    Dim mConnction As New SqlConnection
    Dim m_SqlCommand As SqlCommand
    Public ReadOnly Property getSqlConnection() As SqlConnection
        Get
            Return mConnction
        End Get
    End Property
    Public Sub ConnectDataBase(ByVal IP As String, ByVal Port As Integer, ByVal UserName As String, ByVal Pwd As String, ByVal DataBase As String)
        Try
            With mConnction
                .ConnectionString = "server=" & IP & ";uid=" & UserName & ";pwd=" & Pwd & ";database=" & DataBase
                .Open()
                If m_SqlCommand Is Nothing Then m_SqlCommand = New SqlCommand()
                m_SqlCommand.Connection = mConnction
            End With
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub
    Public Sub Close()
        mConnction.Close()
    End Sub
    Public Sub Dispose()
        mConnction.Dispose()
    End Sub
    Public ReadOnly Property ConnectionState() As ConnectionState
        Get
            Return mConnction.State
        End Get
    End Property
    Public Function Search(ByVal TableName As String, ByVal SqlStr As String) As DataSet
        Try
            Dim Adpter As SqlDataAdapter
            Dim rds As New DataSet
            m_SqlCommand = New SqlCommand(SqlStr, mConnction)
            With m_SqlCommand
                Adpter = New SqlDataAdapter(m_SqlCommand)
                Adpter.Fill(rds, TableName)
                Return rds
            End With
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function
    Public Function Search(ByVal TableName As String) As DataSet
        Try
            Dim Adpter As SqlDataAdapter
            Dim rds As New DataSet
            m_SqlCommand = New SqlCommand("Select * from " & TableName, mConnction)
            With m_SqlCommand
                Adpter = New SqlDataAdapter(m_SqlCommand)
                Adpter.Fill(rds, TableName)
                Return rds
            End With
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function
End Class
